package com.matrix.framework.core.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.web.client.RestClient;

/**
 * AI配置类
 *
 * Copyright © 海平面工作室 版权所有
 *
 * @Author: Leo
 * @Create: 2024-01-22 10:00:00
 * @Since 1.2
 */
@Configuration
public class AiConfiguration {

    /**
     * 配置RestClient.Builder Bean - Ollama需要
     */
    @Bean
    @Primary
    public RestClient.Builder restClientBuilder() {
        return RestClient.builder();
    }

    /**
     * 开发环境ChatClient配置 - 使用Ollama
     */
    @Bean
    @Primary
    @Profile("dev")
    public ChatClient devChatClient(@Qualifier("ollamaChatModel") ChatModel chatModel) {
        return ChatClient.builder(chatModel).build();
    }

    /**
     * 生产环境ChatClient配置 - 使用OpenAI
     */
    @Bean
    @Primary
    @Profile("prod")
    public ChatClient prodChatClient(@Qualifier("openAiChatModel") ChatModel chatModel) {
        return ChatClient.builder(chatModel).build();
    }

    /**
     * 通用ChatClient配置 - 当没有特定profile时使用
     * 优先使用OpenAI，如果不可用则使用Ollama
     */
    @Bean
    @Profile("!dev & !prod")
    public ChatClient defaultChatClient(ChatClient.Builder builder) {
        return builder.build();
    }
}